#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const readline = require('readline/promises');
const file = process.argv[2] ?? __filename;
const input = fs.createReadStream(file);

const rl = readline.createInterface({input,crlfDelay:Infinity});
// 检查文件是否存在
if (!fs.existsSync(file)) {
  console.error(`错误：文件 ${file} 不存在。`);
  process.exit(1);
}

let n = 1;
rl.on('line', line=>{
  console.log(`${n++}\t${line}`);
})
rl.on('end', () => {
  console.log('文件读取完成。');
});

// 错误处理
rl.on('error', error => {
  console.error(`读取文件时发生错误: ${error.message}`);
  process.exit(1);
});
